import { defineStore } from 'pinia'

export const useCounterStore = defineStore({
  id: 'counter',

  state: () => ({
    count: 1
  }),
  
  actions: {
    // 增加
    increment (num: number) {
      // console.log(num)
      this.count += num
    },
    // 减少
    decrement (num: number) {
      this.count -= num
    },
    // 如果是奇数增加
    incrementIfOdd (num: number) {
      if (this.count%2===1) {
        this.increment(num)
      }
    },
    // 延迟1S异步增加
    incrementAsync (num: number) {
      setTimeout(() => {
        this.increment(num)
      }, 1000);
    },
  },

  getters: {
    evenOrOdd (): string {
      return this.count%2 === 1 ? '奇数' : '偶数' 
    }
  }
})